/*
 * CompetenceAnalyzerMainPanel.java
 *
 * Created on May 11, 2010, 2:41:28 PM
 */
package at.tugraz.kmi.css.analyzer.gui;

import java.awt.Color;
import java.awt.Component;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.IOException;
import java.util.ArrayList;

import javax.swing.JComponent;
import javax.swing.JTextField;
import javax.swing.JTree;
import javax.swing.SwingConstants;
import javax.swing.TransferHandler;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.TreeCellRenderer;
import javax.swing.tree.TreeSelectionModel;

import at.tugraz.kmi.css.analyzer.UserCompetence;
import at.tugraz.kmi.css.analyzer.gapvis.JobProfile;
import at.tugraz.kmi.css.analyzer.gapvis.basic.GapProfilePanel;
import at.tugraz.kmi.css.analyzer.gapvis.koichi.KiCurrentCompetenceProfilePane;
import at.tugraz.kmi.css.analyzer.gapvis.koichi.KiTargetCompetenceProfilePane;
import at.tugraz.kmi.css.analyzer.model.CompetenceAnalyzerModel;
import at.tugraz.kmi.css.cbkst2.CbKSTCompetence;
import at.tugraz.kmi.css.cbkst2.CbKSTVertex;
import ca.odell.glazedlists.TreeList;
import ca.odell.glazedlists.TreeList.Node;
import ca.odell.glazedlists.swing.EventListModel;

import com.google.inject.Inject;

/**
 * The Main Pane of the GUI
 * 
 * @author Georg Öttl
 */
public class CompetenceAnalyzerMainPanel extends javax.swing.JPanel implements
		TreeSelectionListener {

	private static final long			serialVersionUID	= 1L;
	private CompetenceAnalyzerModel		model;
	private EventListModel<JobProfile>	jobProfileListModel;

	/** Creates new form CompetenceAnalyzerMainPanel */
	public CompetenceAnalyzerMainPanel() {
		initComponents();
	}

	@Inject
	CompetenceAnalyzerMainPanel(KiTargetCompetenceProfilePane tcp,
			KiCurrentCompetenceProfilePane ccp, GapProfilePanel gcp,
			CompetenceAnalyzerModel model) {
		this.model = model;
		initComponents();
		jTree1.setCellRenderer(new CbKSTTreeListCellRenderer());
		jTree1.addTreeSelectionListener(this);
		jTree1.getSelectionModel().setSelectionMode(
				TreeSelectionModel.SINGLE_TREE_SELECTION);

		currentCompetenceProfilePanel.add(ccp);
		targetCompetenceProfilePanel.add(tcp);
		gapCompetenceProfilePanel.add(gcp);
		// this.jListProfile.setModel(jobProfileListModel);
		// jListProfile.revalidate();

		// INITIALIZE DRAG AND DROP
		jTree1.setTransferHandler(new CompetenceTransferHandler());
		jListProfile.setTransferHandler(new JobProfileExportTransferHandler());
	}

	static class CbKSTTreeListCellRenderer extends JTextField implements
			TreeCellRenderer {

		private static final long	serialVersionUID	= 1L;

		public CbKSTTreeListCellRenderer() {
			setOpaque(true);
			setHorizontalAlignment(SwingConstants.LEFT);
		}

		@Override
		public Component getTreeCellRendererComponent(JTree tree, Object value,
				boolean selected, boolean expanded, boolean leaf, int row,
				boolean hasFocus) {
			if (selected) {
				setBackground(Color.gray);
			} else {
				setBackground(Color.white);
			}
			if (value instanceof Node) {
				Node<CbKSTVertex> n = (Node<CbKSTVertex>) value;
				setText(n.getElement().getName());

			} else {
				setText("Competences");
				this.setSize(100, 25);
			}
			// jTree1.repaint();
			// setIcon(entry.getImage());
			return this;
		}
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
	// <editor-fold defaultstate="collapsed"
	// <editor-fold defaultstate="collapsed"
	// <editor-fold defaultstate="collapsed"
	// <editor-fold defaultstate="collapsed"
	// <editor-fold defaultstate="collapsed"
	// <editor-fold defaultstate="collapsed"
	// <editor-fold defaultstate="collapsed"
	// <editor-fold defaultstate="collapsed"
	// <editor-fold defaultstate="collapsed"
	// <editor-fold defaultstate="collapsed"
	// desc="Generated Code">//GEN-BEGIN:initComponents
	private void initComponents() {

		jPanelMain = new javax.swing.JPanel();
		jPanelActions = new javax.swing.JPanel();
		jButtonFinish = new javax.swing.JButton();
		jPanel2 = new javax.swing.JPanel();
		jPanel1 = new javax.swing.JPanel();
		currentCompetenceProfilePanel = new javax.swing.JPanel();
		targetCompetenceProfilePanel = new javax.swing.JPanel();
		gapCompetenceProfilePanel = new javax.swing.JPanel();
		jPanelNav = new javax.swing.JPanel();
		competencePanel = new javax.swing.JScrollPane();
		jTree1 = new javax.swing.JTree();
		profilePanel = new javax.swing.JScrollPane();
		jListProfile = new javax.swing.JList();

		setName("CompetenceAnalyzer"); // NOI18N
		setPreferredSize(new java.awt.Dimension(800, 445));
		setLayout(new java.awt.BorderLayout());

		jPanelMain.setName("jPanelMain"); // NOI18N
		jPanelMain.setLayout(new java.awt.BorderLayout());

		jPanelActions.setName("jPanelActions"); // NOI18N
		jPanelActions.setLayout(new javax.swing.BoxLayout(jPanelActions,
				javax.swing.BoxLayout.LINE_AXIS));

		jButtonFinish.setText("Finish");
		jButtonFinish.setName("jButtonFinish"); // NOI18N
		jButtonFinish.addActionListener(new java.awt.event.ActionListener() {
			@Override
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jButtonFinishActionPerformed(evt);
			}
		});
		jPanelActions.add(jButtonFinish);

		jPanelMain.add(jPanelActions, java.awt.BorderLayout.SOUTH);

		jPanel2.setName("jPanel2"); // NOI18N
		jPanel2.setLayout(new java.awt.BorderLayout());

		jPanel1.setName("jPanel1"); // NOI18N
		jPanel1.setLayout(new javax.swing.BoxLayout(jPanel1,
				javax.swing.BoxLayout.LINE_AXIS));

		currentCompetenceProfilePanel.setBackground(new java.awt.Color(108, 91,
				254));
		currentCompetenceProfilePanel.setBorder(javax.swing.BorderFactory
				.createTitledBorder("Current Competence Profile"));
		currentCompetenceProfilePanel.setAutoscrolls(true);
		currentCompetenceProfilePanel.setName("currentCompetenceProfilePanel"); // NOI18N
		currentCompetenceProfilePanel.setLayout(new java.awt.BorderLayout());
		jPanel1.add(currentCompetenceProfilePanel);

		targetCompetenceProfilePanel.setBackground(new java.awt.Color(255, 242,
				0));
		targetCompetenceProfilePanel.setBorder(javax.swing.BorderFactory
				.createTitledBorder("Target Competence Profile"));
		targetCompetenceProfilePanel.setAutoscrolls(true);
		targetCompetenceProfilePanel.setName("targetCompetenceProfilePanel"); // NOI18N
		targetCompetenceProfilePanel.setLayout(new java.awt.BorderLayout());
		jPanel1.add(targetCompetenceProfilePanel);

		jPanel2.add(jPanel1, java.awt.BorderLayout.CENTER);

		gapCompetenceProfilePanel.setBackground(new java.awt.Color(43, 255, 0));
		gapCompetenceProfilePanel.setBorder(javax.swing.BorderFactory
				.createTitledBorder("Gap Competence Profile"));
		gapCompetenceProfilePanel.setName("gapCompetenceProfilePanel"); // NOI18N
		gapCompetenceProfilePanel.setPreferredSize(new java.awt.Dimension(0,
				280));
		gapCompetenceProfilePanel.setLayout(new java.awt.BorderLayout());
		jPanel2.add(gapCompetenceProfilePanel, java.awt.BorderLayout.NORTH);

		jPanelMain.add(jPanel2, java.awt.BorderLayout.CENTER);

		add(jPanelMain, java.awt.BorderLayout.CENTER);

		jPanelNav.setName("jPanelNav"); // NOI18N
		jPanelNav.setLayout(new javax.swing.BoxLayout(jPanelNav,
				javax.swing.BoxLayout.Y_AXIS));

		competencePanel.setBorder(javax.swing.BorderFactory.createTitledBorder(
				null, "Competence", javax.swing.border.TitledBorder.CENTER,
				javax.swing.border.TitledBorder.DEFAULT_POSITION));
		competencePanel.setName("competencePanel"); // NOI18N

		jTree1.setModel(model.getCompetenceList());
		jTree1.setDragEnabled(true);
		jTree1.setName("jTree1"); // NOI18N
		jTree1.addMouseListener(new java.awt.event.MouseAdapter() {
			@Override
			public void mouseClicked(java.awt.event.MouseEvent evt) {
				jTree1MouseClicked(evt);
			}
		});
		jTree1.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
			@Override
			public void mouseDragged(java.awt.event.MouseEvent evt) {
				jTree1MouseDragged(evt);
			}
		});
		competencePanel.setViewportView(jTree1);

		jPanelNav.add(competencePanel);

		profilePanel.setBorder(javax.swing.BorderFactory.createTitledBorder(
				null, "Profiles", javax.swing.border.TitledBorder.CENTER,
				javax.swing.border.TitledBorder.DEFAULT_POSITION));
		profilePanel.setName("profilePanel"); // NOI18N
		profilePanel.setPreferredSize(new java.awt.Dimension(179, 350));

		jListProfile.setModel(model.getJobProfileListModel());
		jListProfile.setDragEnabled(true);
		jListProfile.setName("jListProfile"); // NOI18N
		jListProfile.setPreferredSize(new java.awt.Dimension(165, 150));
		profilePanel.setViewportView(jListProfile);

		jPanelNav.add(profilePanel);

		add(jPanelNav, java.awt.BorderLayout.WEST);
	}// </editor-fold>//GEN-END:initComponents

	private void jButtonFinishActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_jButtonFinishActionPerformed
		// TODO add your handling code here:
	}// GEN-LAST:event_jButtonFinishActionPerformed

	private void jTree1MouseDragged(java.awt.event.MouseEvent evt) {// GEN-FIRST:event_jTree1MouseDragged
		// TODO add your handling code here:
	}// GEN-LAST:event_jTree1MouseDragged

	private void jTree1MouseClicked(java.awt.event.MouseEvent evt) {// GEN-FIRST:event_jTree1MouseClicked

		// System.out.println(source);
	}// GEN-LAST:event_jTree1MouseClicked

	// Variables declaration - do not modify//GEN-BEGIN:variables
	private javax.swing.JScrollPane	competencePanel;
	private javax.swing.JPanel		currentCompetenceProfilePanel;
	private javax.swing.JPanel		gapCompetenceProfilePanel;
	private javax.swing.JButton		jButtonFinish;
	private javax.swing.JList		jListProfile;
	private javax.swing.JPanel		jPanel1;
	private javax.swing.JPanel		jPanel2;
	private javax.swing.JPanel		jPanelActions;
	private javax.swing.JPanel		jPanelMain;
	private javax.swing.JPanel		jPanelNav;
	private javax.swing.JTree		jTree1;
	private javax.swing.JScrollPane	profilePanel;
	private javax.swing.JPanel		targetCompetenceProfilePanel;

	// End of variables declaration//GEN-END:variables

	@Override
	public void valueChanged(TreeSelectionEvent e) {
		// System.out.println(e);
		e.getSource();
		// System.out.println(e.getPath());
	}

	private class JobProfileExportTransferHandler extends TransferHandler {

		private static final long	serialVersionUID	= 1L;

		@Override
		protected Transferable createTransferable(JComponent c) {
			Transferable t = new Transferable() {

				@Override
				public boolean isDataFlavorSupported(DataFlavor flavor) {
					boolean equals = flavor.equals(new DataFlavor(
							UserCompetence[].class, "JobProfile"));
					return equals;
				}

				@Override
				public DataFlavor[] getTransferDataFlavors() {
					return new DataFlavor[] { new DataFlavor(
							UserCompetence[].class, "JobProfile") };
				}

				@Override
				public Object getTransferData(DataFlavor flavor)
						throws UnsupportedFlavorException, IOException {
					Object[] selectedValues = jListProfile.getSelectedValues();
					ArrayList<UserCompetence> competences = new ArrayList<UserCompetence>();

					for (Object selectedValue : selectedValues) {
						JobProfile object = (JobProfile) selectedValue;
						competences.addAll(object.getCompetences());
					}

					return competences.toArray();
				}
			};
			return t;
		}

		@Override
		public int getSourceActions(JComponent c) {
			return COPY;
		}

	}

	private class CompetenceTransferHandler extends TransferHandler {
		/**
		 * 
		 */
		private static final long	serialVersionUID	= 1L;

		@Override
		protected Transferable createTransferable(JComponent c) {
			Transferable t = new Transferable() {

				@Override
				public boolean isDataFlavorSupported(DataFlavor flavor) {
					boolean equals = flavor.equals(new DataFlavor(
							UserCompetence[].class, "UserCompetence"));
					return equals;
				}

				@Override
				public DataFlavor[] getTransferDataFlavors() {
					return new DataFlavor[] { new DataFlavor(
							UserCompetence[].class, "UserCompetence") };
				}

				@Override
				public Object getTransferData(DataFlavor flavor)
						throws UnsupportedFlavorException, IOException {
					Object selectedValue = jTree1
							.getLastSelectedPathComponent();
					TreeList.Node node = (TreeList.Node) selectedValue;
					CbKSTCompetence competence = (CbKSTCompetence) node
							.getElement();

					return new UserCompetence[] { new UserCompetence(competence) };
				}
			};
			return t;
		}

		@Override
		public int getSourceActions(JComponent c) {
			return COPY;
		}

	};
}
